<?php
namespace App\Jobs;

use DB;
use App\Jobs\Job;

use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

require __DIR__.'/../Library/CCPRestSDK.php';
//require __DIR__.'/../Libary/Ucpaas.class.php';
use REST;
//use Ucpaas;

class SendIVR extends Job implements SelfHandling, ShouldQueue
{
	protected $id;
	protected $phone;
    protected $sender_phone;
    protected $txt;
    /**
     * Create a new job instance.
     *
     * @param  User  $user
     * @return void
     */
    public function __construct($data)
    {
    	$this->id 					= $data["id"];
    	$this->phone 				= $data["phone"];
        $this->sender_phone         = $data["sender_phone"];
        $this->txt                  = $data["txt"];

    }

    /**
     * Execute the job.
     *
     * @param  Mailer  $mailer
     * @return void
     */
    public function handle()
    {
        //云之讯
        // $options['accountsid']  ='21f2b4a4c1db37d8bea5e37fed8382e3';  //账号ID
        // $options['token']       ='8c73ecf18df5a6d4dd8790db8c51509b';    //开发者Token
        // $ucpass = new Ucpaas($options);
        // $appId="080a691b46dd40e0963d0bd55f84ad84";
        // $to="18233565782";
        // $type="0";
        // $content="测试一下";
        // $toSerNum="15232326681";
        // $playTimes="1";
        // echo $ucpass->voiceNotify($appId,$to,$type,$content,$toSerNum,$playTimes);

        //容联云通讯
        $accountSid     = 'aaf98f894fd44d15014fe540e3021028';//主帐号
        $accountToken   = '845e3e01b63344b493b9a929a00e51db';//主帐号Token
        $appId          = 'aaf98f894fd44d15014fe54303bd1031';//应用Id
        $rest           = new REST(env("IVR_URL"),'8883','2013-12-26');
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);
        
        // 调用IVR外呼接口
        $to         = $this->phone; //被叫号码
        $mediaName  = ""; //语音文件名称，格式 wav。与mediaTxt不能同时为空。当不为空时mediaTxt属性失效。
        $mediaTxt   = $this->txt; //文本内容
        $displayNum = $this->sender_phone; //显示的主叫号码
        $playTimes  = "2"; //循环播放次数，1－3次，默认播放1次。
        $respUrl    = "http://timepcker.cn/api/messagehook/ivr"; //外呼通知状态通知回调地址，云通讯平台将向该Url地址发送呼叫结果通知。
        $userData   = json_encode(["id"=>$this->id]); //用户私有数据
        $maxCallTime= "56"; //最大通话时长
        $speed      = "-150"; //发音速度
        $volume     = "10"; //音量
        $pitch      = "-100"; //音调
        $bgsound    = "0"; //背景音编号

        //发送
        $sendTime = date("Y-m-d H:i:s");
        try{
            $result = $rest->landingCall($to,$mediaName,$mediaTxt,$displayNum,$playTimes,$respUrl,$userData,$maxCallTime,$speed,$volume,$pitch,$bgsound);
            if($result == NULL || $result->statusCode!=0)
                $result = ["send_code"=>"-2","send_time"=>$sendTime,"send_detail"=>json_encode($result)];
            else
                $result = ["send_code"=>"1","send_time"=>$sendTime,"send_detail"=>json_encode($result)];
        }catch(Exception $e){
            $result = ["send_code"=>"-1","send_time"=>$sendTime,"send_detail"=>json_encode($e)];
        }
        DB::table("im_inform_user_method")->where("id",$this->id)->update($result);
        return true; 
    }
}